
/**
** \file       free_parser.c
**
** \brief      this function free the ast printer
**
** \author     Ben M'rad Hanna, Laterem Hamza, Boillot Ulysse, De Olivera
**             Guillaume and Nguyen Viet Dung
**
** \version    1.0
**
** \date       28 Novembre 2010
**
*/

#include "free_parser.h"

void		free_dogroup(s_dogroup		*dogroup)
{
  if (dogroup != NULL)
  {
    if (dogroup->todo != NULL)
    {
      if (dogroup->todo->andor == NULL)
        free(dogroup->todo);
      else
      {
        free_andor(dogroup->todo->andor);
        free(dogroup->todo);
      }
    }

    free(dogroup);
  }
}

void		free_andor(s_andor	*andor)
{
  s_pipeline	*aux = NULL;
  s_pipeline	*tmp = NULL;

  if (andor != NULL)
  {
    if (andor->pl != NULL)
    {
      tmp = andor->pl;

      while (tmp != NULL)
      {
        aux = tmp;
        tmp = tmp->next;
        free_cmd(aux->cmd);
        free(aux->no);
        free(aux);
      }
    }

    free(andor->and_or);
    free(andor);
  }
}

void		free_ast(s_ast			*ast)
{
  s_andor	*aux = NULL;
  s_andor	*tmp = NULL;

  if (ast != NULL)
    {
      if (ast->list != NULL)
	{
	  tmp = ast->list->andor;

	  while (tmp != NULL)
	    {
	      aux = tmp;
	      tmp = tmp->next;
	      free_andor(aux);
            }

	  free(ast->list);
	}

      free(ast);
    }
}

void            free_word(s_word        *word)
{
  if (word != NULL)
  {
    if (word->next == NULL)
    {
      free(word->word);
      free(word);
    }
    else
    {
      free_word(word->next);
      free(word->word);
      free(word);
    }
  }
}
